<?php
// Open Header, include functions and get the showid and cached xml
include 'functions.php';
loadheader();
$loc = $_GET['loc'];

$cache_time = 3600*24; // 24 hours

$cache_file = './cache/countdown.xml';
$timedif = @(time() - filemtime($cache_file));

if (file_exists($cache_file) && $timedif < $cache_time) {
    $string = file_get_contents($cache_file);
} else {
    $string = file_get_contents('http://services.tvrage.com/feeds/countdown.php');
    if (($string) && ($f = @fopen($cache_file, 'w'))) {
        fwrite ($f, $string, strlen($string));
        fclose($f);
    }
}
// Cached the file, now lets use it.
if (!file_exists($cache_file)) { // Does it exist? If it doesn't there's a problem so lets report it.
 echo "<center><h2>Cache file issue: Cannot open cached XML poss connection issue?</h2></center>";
 break;
}

$xml = simplexml_load_file($cache_file);

echo "<div class=\"showtitle\">Current Countdown List for ".$loc."</div>\n";
echo "<table border=0 width=600px; cellpadding=2>\n";
echo "<tr><th width=30px;>Show:</th><th width=70px;>Episode:</th><th width=80px;>Season<br>Ep#</th><th width=80px;>Air Date</th><th>Relative Time: (From Cache File Creation)</th></tr>\n";
echo "<tr></tr>\n";
foreach($xml->country as $country){
 	echo "<tr><th colspan=\"5\"><b>Country List of: ".$country['name']."</b></th></tr>\n";
 	foreach($country->show as $node){
 		echo "<tr><td><a href=eplist.php?sid=".$node->showid.">".$node->showname."</a></td>\n";
 		echo "<td><a href=\"".$node->upcomingep->link."\">".make_safe($node->upcomingep->title)."</a></td>\n";
 		echo "<td>".$node->upcomingep->epnum."</td>\n";
 		echo "<td>".$node->upcomingep->airdate."</td>\n";
		echo "<td>".$node->upcomingep->relativedate."</td></tr>\n";
 	  
 	}
}echo "</table>\n";
echo "<p><div align=center>Page generated using a cached feed generated on: ".date ("F d Y H:i:s.", filemtime($cache_file))."</div></p>\n";


loadfooter();	

?>